package com.sdsy.controller;


/**
 * (TransferAccountsRecord)表控制层
 *
 * @author yxc
 * @since 2018-09-03 11:52:02
 */

import cn.hutool.json.JSONUtil;
import com.sdsy.constant.WalletConfig;
import com.sdsy.service.TransferAccountsRecordService;
import com.sdsy.util.RecreationChainUtil;
import com.sdsy.util.SysUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;


@RestController()
@RequestMapping("/transferAccountsRecord")
//@PropertySource("classpath:config/config.properties")
public class TransferAccountsRecordController {
    private static Logger logger = LoggerFactory.getLogger(TransferAccountsRecordController.class);

    @Autowired
    private WalletConfig walletConfig;
    @Autowired
    private TransferAccountsRecordService transferAccountsRecordService;



    /**
     * 查询用户代币余额
     *
     * @param entity
     */
    @PostMapping("/selectDbMenoy")
    public ResponseEntity selectDbMenoy(@RequestBody Map<String, Object> entity) {
        BigInteger all = new BigInteger("0");
        try {
            RecreationChainUtil recreationChainUtil = (RecreationChainUtil) WalletConfig.map.get("keySCode");
            if (SysUtil.isNull(recreationChainUtil)) {
                return new ResponseEntity("操作失败", HttpStatus.INTERNAL_SERVER_ERROR);
            }
            String address = (String) entity.get("address");
            all = recreationChainUtil.balanceOf(address).send();
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("selectDbMenoy异常============" + e.getMessage(), e);
        }
        BigDecimal a = new BigDecimal(all);
        BigDecimal b = new BigDecimal("1000000000000000000");
        BigDecimal c = a.divide(b);
        String db = c.stripTrailingZeros().toPlainString();
        System.out.println("db==================" + db);
        Map<String,Object> map =new HashMap<String,Object>();
        map.put("data",db);
        map.put("message","成功");
        return new ResponseEntity(JSONUtil.toJsonStr(map), HttpStatus.OK);
    }


    @PostMapping("/save")
    public  void save(@RequestBody Map<String, Object> entity){
        System.out.println("--------------");
    }
}